//
//  BoatComplaintViewController.m
//  shuiyun
//
//  Created by 徐双 on 2017/9/7.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "BoatComplaintViewController.h"
#import "BoatComplaintViewCell.h"
#import "ComplaintDetailViewController.h"

#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
@interface BoatComplaintViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property(nonatomic,strong)NSMutableArray * dataArr;

@property (nonatomic, assign) NSInteger page;
@end

@implementation BoatComplaintViewController


- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    __weak typeof(self) weakself = self;
    [self creationData];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [weakself creationDataJZ];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakself creationData];
        
        [self.tableView.mj_header beginRefreshing];
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
     _page = 1;    
    [self setTitle:@"船舶投诉"];
    
    //初始化tableView
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 );
    
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.estimatedRowHeight = 800;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    
    [self.view addSubview:self.tableView];
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BoatComplaintViewCell" bundle:nil] forCellReuseIdentifier:@"BoatComplaintViewCell"];
    
}




- (void)creationData{
    
    //  _dataArr = [NSMutableArray array];
    
    
    
    [self.tableView.mj_header endRefreshing];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    _page = 1;
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    //    NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    NSString *userid = [PublicFunction getConfigWithKey:@"userid"];
    
    [productParams setValue:userid forKey:@"userid"];
    
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:strPage forKey:@"p"];
    
    
    [[MNetworkHandler sharedInstance]conURL:boatComplaint networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            self.dataArr = [returnData objectForKey:@"data"];
            
            
            NSLog(@"DDDDDDDDDDDD%@", self.dataArr);
            
            [self.tableView reloadData];
            
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [hud removeFromSuperview];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        [hud removeFromSuperview];
    }];
    
}
- (void)creationDataJZ{
    
    _page++;
    
    
    NSString *strPage = [NSString stringWithFormat:@"%ld", (long)_page];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    //    NSString *str = @"917b0ddccade4345b1b066fdc4971a14";
    NSString *userid = [PublicFunction getConfigWithKey:@"userid"];
    
    [productParams setValue:userid forKey:@"userid"];
    
    [productParams setValue:token forKey:@"token"];
    [productParams setValue:strPage forKey:@"p"];
   
    
    [[MNetworkHandler sharedInstance]conURL:boatComplaint networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            NSMutableArray *arr =[returnData objectForKey:@"data"];
            
            if (arr.count == 0) {
                
                [self.tableView.mj_footer endRefreshing];
                
            }else {
                
                [self.dataArr addObjectsFromArray:arr];
                
                NSLog(@"JZJZJZJZJZJJZJZJZJZJZJJZJZ%@", self.dataArr);
                
                
            }
            
            
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            [self.tableView.mj_footer endRefreshing];
            
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
    }];
    
}



-(CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return UITableViewAutomaticDimension;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BoatComplaintViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BoatComplaintViewCell"];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    
    NSDictionary * dict = self.dataArr[indexPath.row];
    
    cell.contentLabel.text = [dict objectForKey:@"content"];
    
    cell.nameLabel.text = [dict objectForKey:@"defendant"];
    
    cell.timeLabel.text = [dict objectForKey:@"s_time"];
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ComplaintDetailViewController * vc = [[ComplaintDetailViewController alloc] init];

    vc.hidesBottomBarWhenPushed = YES;
    
    NSDictionary * dict = self.dataArr[indexPath.row];
    
    vc.idStr = [dict objectForKey:@"id"];
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
